473,480 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::string and char*, what am i doing wrong?

Hi,

I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}

// but the value passed to oldval is wrong, oldval gets allocated some
garbage.
// what am i missing?

Many thanks

Sims
Jul 22 '05 #1
11 2464

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...
Hi,

I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp
How did you "get some text" into temp? Try to use a mechanism like this:

std::strcpy(tmp,"Good");
// you will need to inlcude <cstring> for this

// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}

// but the value passed to oldval is wrong, oldval gets allocated some
garbage.
// what am i missing?

Many thanks

Sims


Regards,
Sumit.
Jul 22 '05 #2

I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp


How did you "get some text" into temp? Try to use a mechanism like this:


Well, i am using a Window API,

RegQueryValueEx(...) and the value returned is corect.
When i am tracing the code i can see that the value is good.
But as soon as i try to copy it to my std::string it does not work.

Sims
Jul 22 '05 #3

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...

I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp


How did you "get some text" into temp? Try to use a mechanism like this:


Well, i am using a Window API,

RegQueryValueEx(...) and the value returned is corect.
When i am tracing the code i can see that the value is good.
But as soon as i try to copy it to my std::string it does not work.


How about posting the whole code.
May be you can separate out the Windows API with a dummy function and then try
showing the code.
Someone would be then able to help you.

-Sharad
P.S : Try running the code under a debugger.
Jul 22 '05 #4

Well, i am using a Window API,

RegQueryValueEx(...) and the value returned is corect.
When i am tracing the code i can see that the value is good.
But as soon as i try to copy it to my std::string it does not work.
How about posting the whole code.
May be you can separate out the Windows API with a dummy function and then

try showing the code.
Someone would be then able to help you.

It is a window API so i am affraid it would be OT.
All i do is read a registery value. I then copy the data to a std::string
-Sharad
P.S : Try running the code under a debugger.


That is what i did and that is where i noticed the problem.

if i do,

std::string test = "Hello";

I can see in the debugger that test="Hello".

But if i do

char * tmp = new char[10+1];
strcpy( tmp, "Tested" );
test = tmp;

the debugger shows
test == "0x)()!I())@#" // <== Garbage
but is correct and test.c_str() == "Tested";

I am no longer sure if it is a problem, (i am not used to .NET so i don't
know if the values displayed are right).

Sims

Jul 22 '05 #5

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...

Well, i am using a Window API,

RegQueryValueEx(...) and the value returned is corect.
When i am tracing the code i can see that the value is good.
But as soon as i try to copy it to my std::string it does not work.


How about posting the whole code.
May be you can separate out the Windows API with a dummy function and then

try
showing the code.
Someone would be then able to help you.


It is a window API so i am affraid it would be OT.
All i do is read a registery value. I then copy the data to a std::string
-Sharad
P.S : Try running the code under a debugger.


That is what i did and that is where i noticed the problem.

if i do,

std::string test = "Hello";

I can see in the debugger that test="Hello".


Try this out -
After using the Windows API you must be getting the data to be copied into a
buffer.
Print that out before copying to the string.
I think most probably you are not using the API correctly.
If you are unsure as to what the debugger is showing , use simple print
statements and verify that the data is indeed valid.
..

Jul 22 '05 #6
>
Try this out -
After using the Windows API you must be getting the data to be copied into a buffer.
Print that out before copying to the string.
I think most probably you are not using the API correctly.
If you are unsure as to what the debugger is showing , use simple print
statements and verify that the data is indeed valid.


No, it is printing it out ok. The value is fine, (i can compare it with the
reg value).
From what i can see it is the debugger that is not displaying it right.

if i do

std::string a = "Hello"; // displays ok
std::string b = tmp; // only displays b.c_str() ok, (when tmp is a
char*).

I still have a nagging feeling that i am not doing something right thought.
I cannot see the MS boys overlooking that case.

Sims

Jul 22 '05 #7
perhaps your debugger is not interpreting the string properly and just
printing its address (the stuff starting 0x...). gdb does this for
std::string cos it doesn't know that a std::string is a string.
Jul 22 '05 #8
"Alex Tibbles" <al**********@yahoo.co.uk> wrote in message news:<40***********************@mercury.nildram.ne t>...
perhaps your debugger is not interpreting the string properly and just
printing its address (the stuff starting 0x...). gdb does this for
std::string cos it doesn't know that a std::string is a string.


I suspect that's the problem here. Your code seems correct, and
the value of the std::string is actually correct, but it looks
wrong in your debugger.

I've experienced this myself in Visual Studio 7, but it was fixed
in 7.1 if I recall correctly. The issue is that beginning in v 7,
the implementation of std::basic_string (from Dinkumware) used the
small string optimization. If you look at the source, you'll see
the following union:

union _Bxty
{ // storage for small buffer or pointer to larger one
_Elem _Buf[_BUF_SIZE];
_Elem *_Ptr;
} _Bx;

A short string is stored in _Buf, in which case _Ptr is garbage.
The debugger took a while to catch up with this. So in 7.0 (but
not IIRC in 7.1) the debugger only knew about _Ptr. So for a
small string, the debugger displayed the contents of a "random"
location.

<sop_to_OT_police>
If you think about it, this is kind of a tricky thing for a
debugger to get right so you may see similar behavior on other
implementations that use this technique.
</sop_to_OT_police> :-)
Jul 22 '05 #9

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:bu************@ID-221354.news.uni-berlin.de...
May be you can separate out the Windows API with a dummy function and then try showing the code.

this would certainly help to avoid offending some people which do
programming without an underlying os -- come real problems require an
underlying OS
Jul 22 '05 #10

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...
void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}


First using a writeable reference is kind of code obfuscation -- some people
may disagree here.
Why don't you use a fixed size buffer -- this would avoid all the problems
with allocating some buffer.

void Function(std::string *pOldValue)
{
char ac[1024];

....

*pOldValue = ac;
}

If you really need a dynamic buffer, use
std::vector<char> sBuffer(someSize);

This avoids that you have to manually destroy the buffer.
It is also exception safe
-- remember that between new and delete somebody may throw an exception.
Jul 22 '05 #11

"Frank Puck" <ne***********@sbcglobal.net> wrote in message
news:B4******************@newssvr25.news.prodigy.c om...

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...
void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}


Why not just put the data directly into oldval? std::string is very clean
relative to raw char*'s. std::string will dynamically resize for you, so
you don't need to worry about buffer sizes (don't use std::vector<char> as
someone else suggested).

std::cin >> oldVal; // one possibility from standard input
oldVal = *(someCFunctionThatReturnsCharPointer( ));

Most likely, you can get rid of this Function altogether and simply use a
std::string at the call site. It looks like you are worried about making
conversions between char* and std::string - you don't need to worry about
such things.

Good Luck,
Shane
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3379
by: James Vanns | last post by:
I want to be able to print out (and read in) characters with accents (for example French and Italian text). So far I have this: std::locale lang (getenv ("LANG")); which seems to set the...
9
3278
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
2
1913
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
6
4841
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
5
9177
by: Tom Smith | last post by:
I hardly dare ask this given the furore in another thread over strings and const... My problem is this. I am assured that casting away the constness of the return value of std::string::c_str() is...
5
2352
by: Stefan Naewe | last post by:
A simple test: <----------------------------> #include <string> #include <iostream> int main() { std::string h("Hello "); *h.rbegin() = '\0';
11
2875
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
29
23114
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
5
1970
by: Olaf | last post by:
Hi, I wrap a legacy C library, e.g. the signature is void set_error_buffer(char* buf); where the buf length should be of length of 512 (it's defined). Now I want to wrap it with...
0
7065
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6924
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
5376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4511
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3028
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1325
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
219
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.